package cn.myfirefly.moming.house.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import cn.myfirefly.moming.house.entity.SpfTradeContractWebimage;
import cn.myfirefly.moming.house.model.SpfTradeContractDetailDto;
import cn.myfirefly.moming.house.model.SpfTradeContractWebimageDto;
import cn.myfirefly.moming.house.model.SpfTradeTrackFormDto;
import cn.myfirefly.moming.house.model.SpfTradeTrackFormFilterDto;
import cn.myfirefly.moming.house.model.SpfTradeWageDto;
import cn.myfirefly.moming.house.service.SpfTradeContractService;
import cn.myfirefly.moming.house.service.SpfTradeContractWebimageService;
import cn.myfirefly.moming.house.service.SpfTradeTrackFormService;
import cn.myfirefly.moming.house.service.SpfTradeWageService;
import cn.myfirefly.core.BaseController;
import cn.myfirefly.core.Const;
import cn.myfirefly.core.annotation.BjuiDataGridFilterDtoWrap;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.core.model.ResultDto;
import cn.myfirefly.utils.FileTransportUtil;
import cn.myfirefly.utils.StrUtil;

@Controller
@RequestMapping("spfTradeContractWebimageAudit")
public class SpfTradeContractWebimageAuditController extends BaseController {

	@Autowired
	private SpfTradeContractWebimageService spfTradeContractWebimageService;
	
	@Autowired
	private SpfTradeTrackFormService spfTradeTrackFormService;

	@Autowired
	private SpfTradeContractService spfTradeContractService;
	
	@Autowired
	private SpfTradeWageService spfTradeWageService;
	
	@RequestMapping("index.do")
	public String index(HttpServletRequest request, 
			HttpServletResponse response) {
		return "spftradecontractwebimageaudit/index";
	}
	
	@RequestMapping("imgindex.do")
	public String imgindex(HttpServletRequest request, 
			HttpServletResponse response) {
		request.setAttribute("contractId", request.getParameter("contractId"));
		return "spftradecontractwebimageaudit/imgindex";
	}
	
	@RequestMapping("uploadIndex.do")
	public String uploadIndex(HttpServletRequest request, 
			HttpServletResponse response) {
		request.setAttribute("contractId", request.getParameter("contractId"));
		return "spftradecontractwebimageaudit/upload";
	}
	
	@RequestMapping("edit.do")
	public String edit(HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "spftradecontractwebimageaudit/edit";
	}
	
	@RequestMapping("add.do")
	public String add(HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "spftradecontractwebimageaudit/edit";
	}
	
	@RequestMapping("show.do")
	public String show(HttpServletRequest request, 
			HttpServletResponse response) {
		request.setAttribute("funcId", "spfTradeContractWebimageAudit");
		request.setAttribute("contractId", request.getParameter("contractId"));
		return "spftradecontract/show";
	}
	
	@RequestMapping("wagelist.do")
	@ResponseBody
	public PageResultDto<SpfTradeWageDto> wagelist(SpfTradeWageDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		String contractId = request.getParameter("contractId");
		if (StrUtil.isBlank(contractId)) {
			return new PageResultDto<SpfTradeWageDto>();
		}
		dto.setTradeContractId(new Integer(contractId));
		PageResultDto<SpfTradeWageDto> result = spfTradeWageService.getList(dto);
		return result;
	}
	
	@RequestMapping("back.do")
	@ResponseBody
	public ResultDto back(SpfTradeContractWebimageDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		String contractId = request.getParameter("contractId");
		if (StrUtil.isBlank(contractId)) {
			return error("请求参数错误");
		}
		spfTradeContractWebimageService.back(new Integer(contractId));
		return success("执行成功");
	}
	
	@RequestMapping("pass.do")
	@ResponseBody
	public ResultDto pass(SpfTradeContractWebimageDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		String contractId = request.getParameter("contractId");
		if (StrUtil.isBlank(contractId)) {
			return error("请求参数错误");
		}
		spfTradeContractWebimageService.pass(new Integer(contractId));
		return success("执行成功");
	}
	
	@RequestMapping("saveFileAndInsertInfo.do")
	@ResponseBody
	public ResultDto saveFileAndInsertInfo(HttpServletRequest request, 
			MultipartHttpServletRequest request2) {
		String contractId = request.getParameter("contractId");
		if (StrUtil.isBlank(contractId)) {
			return error("请求参数错误");
		}
		// 上传文件
		MultipartFile file = request2.getFile("file");
		String targetName = FileTransportUtil.saveFile(this.picRealPath+"/" + contractId, file);
		// 保存记录
		SpfTradeContractWebimageDto dto = new SpfTradeContractWebimageDto();
		dto.setImgUrl("/" + contractId + "/" + targetName);
		dto.setTradeContractId(new Integer(contractId));
		warpModifyInfoByName(dto, request2);
		spfTradeContractWebimageService.insert(dto);
		dto.setImgUrl(this.picUrl + dto.getImgUrl());
		return success(dto);
	}
	
	@RequestMapping("editSave.do")
	@ResponseBody
	public ResultDto editSave(SpfTradeContractWebimageDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		if (dto.getId() == null) {
			return error("请求参数错误");
		}
		warpModifyInfo(dto, request);
		spfTradeContractWebimageService.update(dto);
		return success("保存成功");
	}
	
	@RequestMapping("list.do")
	@ResponseBody
	@BjuiDataGridFilterDtoWrap
	public PageResultDto<SpfTradeTrackFormDto> list(SpfTradeTrackFormFilterDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		PageResultDto<SpfTradeTrackFormDto> result = spfTradeTrackFormService.getToBeAuditedList(dto);
		return result;
	}
	
	@RequestMapping("imglist.do")
	@ResponseBody
	public ResultDto imglist(HttpServletRequest request, 
			HttpServletResponse response) {
		String contractId = request.getParameter("contractId");
		if (StrUtil.isBlank(contractId)) {
			return error("参数格式不正确");
		}
		List<SpfTradeContractWebimage> list = spfTradeContractWebimageService.getList(new Integer(contractId));
		for (SpfTradeContractWebimage spfTradeContractWebimage : list) {
			spfTradeContractWebimage.setImgUrl(this.picUrl + spfTradeContractWebimage.getImgUrl());
		}
		return success(list);
	}
	
	@RequestMapping("get.do")
	@ResponseBody
	public ResultDto getSpfTradeContractWebimage(SpfTradeContractWebimageDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		SpfTradeContractWebimageDto spfTradeContractWebimage = spfTradeContractWebimageService.getSpfTradeContractWebimage(dto.getId());
		return success(spfTradeContractWebimage);
	}
	
	@RequestMapping("getDetail.do")
	@ResponseBody
	public ResultDto getContractDetail(HttpServletRequest request, 
			HttpServletResponse response) {
		String id = request.getParameter("id");
		if (StrUtil.isBlank(id)) {
			return error("请求参数错误");
		}
		SpfTradeContractDetailDto dto = spfTradeContractService.getContractDetail(new Integer(id));
		return success(dto);
	}
	
	@RequestMapping("del.do")
	@ResponseBody
	public ResultDto del(SpfTradeContractWebimageDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		String msg = spfTradeContractWebimageService.del(dto.getId());
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success("删除成功");
	}
}
